package org.hawk.gwt.ppc.parser;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

import org.hawk.gwt.ppc.parser.impl.TokenBasedJavaParser;
import org.hawk.gwt.ppc.utils.PreprocessorException;

/**
 * Entry point of parsing API. Parses compilation unit represented  
 * as input stream or reader to the {@link CompilationUnitDescriptor}.
 * 
 * @author alex.bereznevatiy@gmail.com
 *
 */
public abstract class JavaParser {
	
	protected JavaParser() {
	}
	
	/**
	 * Creates parser based on input stream.
	 * Input stream will be read using UTF-8 encoding.
	 * @param compiation unit as input stream
	 */
	public static JavaParser create(InputStream input) {
		return create(new InputStreamReader(input, Charset.forName("UTF-8")));
	}
	
	/**
	 * Creates parser using characters reader.
	 * @param compiation unit as reader
	 */
	public static JavaParser create(Reader reader) {
		return new TokenBasedJavaParser(reader);
	}
	
	/**
	 * Parses compilation unit and returns descriptor for it.
	 * Note that associated reader or stream should be closed by client.
	 * @return compilation unit descriptor.
	 * @throws PreprocessorException in case of troubles.
	 */
	public abstract CompilationUnitDescriptor parse() throws PreprocessorException;
}
